package community.hospital.controller;

import cn.hutool.core.util.ObjectUtil;
import community.hospital.annotation.NeedLogin;
import community.hospital.annotation.NeedPermission;
import community.hospital.common.R;
import community.hospital.service.application.AnalyseApplicationService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;

import static community.hospital.common.PermissionConstant.SUPER_ADMIN;
import static community.hospital.common.RedisConstant.SPLIT;

/**
 * @Author：ydj
 * @Date：2023/3/12 16:03
 */
@RestController
@RequestMapping("/analyse")
@AllArgsConstructor
public class AnalyseController {
    private final AnalyseApplicationService analyseApplicationService;

    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN)
    @PostMapping("/trafficTrends")

    public R<List<Integer>> trafficTrends() {
        List<Integer> visits = analyseApplicationService.trafficTrends();
        return R.ok(visits);
    }

    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN)
    @GetMapping("/visit")
    public R<List<Integer>> visits() {
        List<Integer> visits = analyseApplicationService.visits();
        return R.ok(visits);
    }

    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN)
    @GetMapping("/visitsByMonth")
    public R<?> getVisitsByMonth() {
        long count = analyseApplicationService.visits().stream()
                .filter(ObjectUtil::isNotNull)
                .reduce(Integer::sum).orElse(0);
        return R.ok(count);
    }

    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN)
    @GetMapping("/countPriceByMonth")
    public R<?> countPriceByMonth() {
        long count = analyseApplicationService.countPrice();
        return R.ok(count);
    }

    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN)
    @GetMapping("/countVolume")
    public R<?> countVolume() {
        long count = analyseApplicationService.countVolume();
        return R.ok(count);
    }

}
